/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package eistar.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.impl.EObjectImpl;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EObjectContainmentEList;
import org.eclipse.emf.ecore.util.EObjectResolvingEList;
import org.eclipse.emf.ecore.util.InternalEList;

import eistar.EistarPackage;
import eistar.Entity;
import eistar.ImportRelation;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Entity</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link eistar.impl.EntityImpl#getName <em>Name</em>}</li>
 *   <li>{@link eistar.impl.EntityImpl#getText <em>Text</em>}</li>
 *   <li>{@link eistar.impl.EntityImpl#getChildren <em>Children</em>}</li>
 *   <li>{@link eistar.impl.EntityImpl#getRefs <em>Refs</em>}</li>
 *   <li>{@link eistar.impl.EntityImpl#getImports <em>Imports</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class EntityImpl extends EObjectImpl implements Entity {
	/**
	 * The default value of the '{@link #getName() <em>Name</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getName()
	 * @generated
	 * @ordered
	 */
	protected static final String NAME_EDEFAULT = null;

	/**
	 * The cached value of the '{@link #getName() <em>Name</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getName()
	 * @generated
	 * @ordered
	 */
	protected String name = NAME_EDEFAULT;

	/**
	 * The default value of the '{@link #getText() <em>Text</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getText()
	 * @generated
	 * @ordered
	 */
	protected static final String TEXT_EDEFAULT = null;

	/**
	 * The cached value of the '{@link #getText() <em>Text</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getText()
	 * @generated
	 * @ordered
	 */
	protected String text = TEXT_EDEFAULT;

	/**
	 * The cached value of the '{@link #getChildren() <em>Children</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getChildren()
	 * @generated
	 * @ordered
	 */
	protected EList<Entity> children;

	/**
	 * The cached value of the '{@link #getRefs() <em>Refs</em>}' reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getRefs()
	 * @generated
	 * @ordered
	 */
	protected EList<Entity> refs;

	/**
	 * The cached value of the '{@link #getImports() <em>Imports</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getImports()
	 * @generated
	 * @ordered
	 */
	protected EList<ImportRelation> imports;

	private Object id;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected EntityImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return EistarPackage.Literals.ENTITY;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public String getName() {
		return name;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setName(String newName) {
		String oldName = name;
		name = newName;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, EistarPackage.ENTITY__NAME, oldName, name));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public String getText() {
		return text;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setText(String newText) {
		String oldText = text;
		text = newText;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, EistarPackage.ENTITY__TEXT, oldText, text));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<Entity> getChildren() {
		if (children == null) {
			children = new EObjectContainmentEList<Entity>(Entity.class, this, EistarPackage.ENTITY__CHILDREN);
		}
		return children;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<Entity> getRefs() {
		if (refs == null) {
			refs = new EObjectResolvingEList<Entity>(Entity.class, this, EistarPackage.ENTITY__REFS);
		}
		return refs;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<ImportRelation> getImports() {
		if (imports == null) {
			imports = new EObjectContainmentEList<ImportRelation>(ImportRelation.class, this, EistarPackage.ENTITY__IMPORTS);
		}
		return imports;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public String getQualifiedName() {
		return eContainer instanceof Entity ? 
				((Entity)eContainer()).getQualifiedName() + "." + getName() : getName();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public Entity getChildEntity(String name) {
		for (Entity child: getChildren())
			if (name.equals(child.getName()))
				return child;
		return null;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	@SuppressWarnings("unchecked")
	public Entity getEntity(String qualifiedName) {
		Entity result = null;
		String[] parts = qualifiedName.split("\\.");
		// first find the situation 
		Resource res = this.eResource();
		if (res != null) {
			for(EObject sitObj: res.getContents()) 
				if (sitObj instanceof Entity && ((Entity) sitObj).getName().equals(parts[0])) {
					result = (Entity) sitObj;
					for (int i = 1; i< parts.length && result != null; i++)
						result = result.getChildEntity(parts[i]);
					break;
				}
		}
		return result;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	@SuppressWarnings("unchecked")
	public <T extends Entity> List<T> getMembers(Class<? extends T> eClass) {
		List<T> result = new ArrayList<T>();
		for(Entity child: getChildren())
			if (eClass.isInstance(child))
				result.add((T) child);
		
		for(Entity child: getRefs())
			if (eClass.isInstance(child))
				result.add((T) child);
		return result;
	}
	
	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	@SuppressWarnings("unchecked")
	public <T extends Entity> List<T> getChildren(Class<? extends T> eClass) {
		List<T> result = new ArrayList<T>();
		for(Entity child: getChildren())
			if (eClass.isInstance(child))
				result.add((T) child);
		return result;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public boolean isReferredTo(Entity entity, int role) {
		if ((role & REFER_TO_ME) > 0) {
			boolean result = entity.isReferredTo(this, role & (~REFER_TO_ME));
			return result;
		}
		return false;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	@SuppressWarnings("unchecked")
	public <T extends Entity> List<T> getReferences(Class<? extends T> eClass) {
		List<T> result = new ArrayList<T>();
		for(Entity child: getRefs())
			if (eClass.isInstance(child))
				result.add((T) child);
		return result;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public boolean isReferredTo(Entity entity) {
		return isReferredTo(entity, REFER_TO_ME);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case EistarPackage.ENTITY__CHILDREN:
				return ((InternalEList<?>)getChildren()).basicRemove(otherEnd, msgs);
			case EistarPackage.ENTITY__IMPORTS:
				return ((InternalEList<?>)getImports()).basicRemove(otherEnd, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case EistarPackage.ENTITY__NAME:
				return getName();
			case EistarPackage.ENTITY__TEXT:
				return getText();
			case EistarPackage.ENTITY__CHILDREN:
				return getChildren();
			case EistarPackage.ENTITY__REFS:
				return getRefs();
			case EistarPackage.ENTITY__IMPORTS:
				return getImports();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case EistarPackage.ENTITY__NAME:
				setName((String)newValue);
				return;
			case EistarPackage.ENTITY__TEXT:
				setText((String)newValue);
				return;
			case EistarPackage.ENTITY__CHILDREN:
				getChildren().clear();
				getChildren().addAll((Collection<? extends Entity>)newValue);
				return;
			case EistarPackage.ENTITY__REFS:
				getRefs().clear();
				getRefs().addAll((Collection<? extends Entity>)newValue);
				return;
			case EistarPackage.ENTITY__IMPORTS:
				getImports().clear();
				getImports().addAll((Collection<? extends ImportRelation>)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case EistarPackage.ENTITY__NAME:
				setName(NAME_EDEFAULT);
				return;
			case EistarPackage.ENTITY__TEXT:
				setText(TEXT_EDEFAULT);
				return;
			case EistarPackage.ENTITY__CHILDREN:
				getChildren().clear();
				return;
			case EistarPackage.ENTITY__REFS:
				getRefs().clear();
				return;
			case EistarPackage.ENTITY__IMPORTS:
				getImports().clear();
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case EistarPackage.ENTITY__NAME:
				return NAME_EDEFAULT == null ? name != null : !NAME_EDEFAULT.equals(name);
			case EistarPackage.ENTITY__TEXT:
				return TEXT_EDEFAULT == null ? text != null : !TEXT_EDEFAULT.equals(text);
			case EistarPackage.ENTITY__CHILDREN:
				return children != null && !children.isEmpty();
			case EistarPackage.ENTITY__REFS:
				return refs != null && !refs.isEmpty();
			case EistarPackage.ENTITY__IMPORTS:
				return imports != null && !imports.isEmpty();
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (name: ");
		result.append(name);
		result.append(", text: ");
		result.append(text);
		result.append(')');
		return result.toString();
	}

	@Override
	public Object getId() {
		if (this.id == null)
			this.id = new Object();
		return id;
	}
	
	@Override
	public void setId(Object newId) {
		this.id = newId;
	}

	@Override
	public Entity clone() {
		try {
			Entity result = this.getClass().newInstance();
			result.setId(this.getId());
			result.setName(this.getName());
			result.setText(this.getText());
			for(Entity child: getChildren())
				result.getChildren().add(child.clone());
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return null;
	}

	
	@Override
	public int compareTo(Entity o) {
		if (o != null)
			return this.hashCode() - o.hashCode();
		return 1;
	}
} //EntityImpl
